<template>
	<view class="jh_content">
		<view class="purpose">
			<view class="title">| 上传证件照图片（正反面）</view>
			<view class="imagView">
				<view class="imgItem" v-for="(item, index) in imgList" :key="index">
					<image class="image" @click="imgInfo(index)" :src="item.path" mode="aspectFill"></image>
					<image class="imgDel" @click="delImg(index)" src="../../../static/mine/delete.png"></image>
				</view>
				<view v-if="imgList.length < 2 " class="imgItem" @click="openCamera">
					<image class="image" src="../../../static/mine/add.png"></image>
					<!-- <u-icon style="margin-left: 30rpx;margin-top: 30rpx;" name="plus" color="#7f8c8d" size="80"></u-icon> -->
				</view>
			</view>
		</view>
		<u-form class="jh_FormBox" :model="form">
			<u-form-item style="margin-left: 10rpx;" :label-position="labelPosition" label="证件号码" label-width="150" prop="IDCode">
				<u-input :border="border" type="password" v-model="form.IDCode" placeholder="请输入证件号"></u-input>
			</u-form-item>
			<u-form-item style="margin-left: 10rpx;" :label-position="labelPosition" label="手机号" label-width="150" prop="phone">
				<u-input :border="border" type="password" v-model="form.phone" placeholder="请输入手机号"></u-input>
			</u-form-item>
			<u-form-item style="margin-left: 10rpx;" :label-position="labelPosition" label="验证码" prop="code" label-width="150">
				<u-input :border="border" placeholder="请输入验证码" v-model="form.code" type="text"></u-input>
				<u-button slot="right" type="success" style="margin-right: 10rpx;" size="mini" @click="getCode">{{codeTips}}</u-button>
			</u-form-item>
		</u-form>
		<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
		<view style="padding:20upx 0;margin-top: 20rpx;">
			<button @click="submitState" class="jh_button">
				<text style="color: #FFFFFF;">提交认证</text>
			</button>
		</view>
	</view>
</template>

<script>
	var _self;
	import {
		pathToBase64,
		base64ToPath
	} from '@/components/imageTool/gsq-image-tools/image-tools/index.js';
	export default {
		data() {
			return {
				form:{
					phone:'',
					IDCode:'',
					code:''
				},
				labelPosition: 'left',
				border: false,
				codeTips:'',
				imgList:[],
				imgCount:1,
				totalImgCount:3,
				base64Array:[]
			}
		},
		onLoad() {
			_self = this;
		},
		methods: {
			//-----------------------------显示照片-----------------------------
			imgInfo:function(index){
				let tempList = [];
				_self.imgList.forEach(img => {
					tempList.push(img.path);
				});
				console.log('添加照片', _self.imgList)
				//显示图片
				uni.previewImage({
					current: index,
					loop: false,
					urls: tempList,
					indicator: 'default'
				});
			},
			//-----------上传图片----------------------------------------
			openCamera:function() {
				//选择照片
				uni.chooseImage({
					count: _self.imgCount,
					sizeType: ['compressed'],
					success: e => {
						_self.imgList = [..._self.imgList, ...e.tempFiles]
						_self.imgCount = _self.totalImgCount - _self.imgList.length;
						for (var i = 0; i < _self.imgList.length; i++) {
							_self.imagePath = _self.imgList[i].path
							pathToBase64(_self.imagePath)
								.then(base64 => {
									_self.base64Array = (base64)
									// console.log(that.imagePath, that.base64Array, '图片');
								})
								.catch(error => {
									console.error('错误', error)
								}
							)
						}
					}
				});
			},
			//-----------------------------删除照片-----------------------------
			delImg(i) {
				uni.showModal({
					content: '确定删除这张吗',
					success: res => {
						if (res.confirm) {
							_self.imgList.splice(i, 1);
							_self.imgCount++;
							for (var i = 0; i < _self.imgList.length; i++) {
								var imagePath = _self.imgList[i].path
								pathToBase64(imagePath)
									.then(base64 => {
										_self.base64Array.push(base64)
									})
									.catch(error => {
										console.error('错误', error)
									})
							}
						} else if (res.cancel) {
			
						}
					}
				});
			},
			//-----------------------------获取验证码-----------------------------
			getCode:function(){
				console.log('获取验证码');
				if(_self.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						_self.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						_self.$refs.uCode.start();
					}, 2000);
				} else {
					_self.$u.toast('倒计时结束后再发送');
				}
			},
			//-----------------------------点击获取验证码-----------------------------
			codeChange(text) {
				_self.codeTips = text;
			},
			//-----------------------------提交修改-----------------------------
			submitState:function(){
				if(_self.imgList.length == 0 || _self.imgList.length == 1){
					if(_self.imgList.length == 0){
						_self.$u.toast('证件照片不能为空');
					}else if(_self.imgList.length == 1){
						_self.$u.toast('请上传完整的证件照');
					}
				}else if(!_self.form.IDCode || !_self.form.phone){
					if(!_self.form.IDCode){
						_self.$u.toast('证件号不能为空');
					}else if(!_self.form.phone){
						_self.$u.toast('手机号码不能为空');
					}
				}else if(!_self.form.code){
					_self.$u.toast('请输入验证码');
				}else if(_self.form.code != '123'){
					_self.$u.toast('输入的验证码有误，请重新输入');
				}else {
					console.log('修改成功');
					uni.navigateBack();
				}
			}
		},
	}
</script>

<style lang="scss">
	.purpose {
		background: #FFFFFF;
		margin: 20upx;
		border-radius: 20upx;
		box-shadow: 0px 2px 5px 0px #AAAAAA;
	}
	.title {
		padding-top: 28upx;
		padding-bottom: 28upx;
		padding-left: 24upx;
		color: #27ae60;
		font-size: 30upx;
		font-weight: bold;
	}
	.imagView {
		display: flex;
		margin: 10rpx;
		padding-left: 10rpx;
		flex-direction: row;
		flex-wrap: wrap;
	}
	.image {
		width: 200rpx;
		height: 200rpx;
		margin-left: 20rpx;
		border-radius: 24rpx;
		margin-top: -4px;
	}
	.imgDel {
		position: absolute;
		width: 40rpx;
		height: 40rpx;
		right: -17rpx;
		top: -5rpx;
		border-radius: 20rpx;
	}
	.imgItem {
		width: 200rpx;
		height: 200rpx;
		margin-right: 20rpx;
		margin-bottom: 20rpx;
		border-radius: 24rpx;
		position: relative;
	}
</style>
